﻿/******************************************************
 *     Torrent Zip File C# Version                    *
 *     Contact: gordon@romvault.com                   *
 *                                                    *
 *     This code is published to expand the           *
 *     availability and usage of torrent zip files.   *
 *     Please feel free to use this code or just      *
 *     learn what you need from it to complete your   *
 *     own torrent zip compatible applications.       *
 *                                                    *
 *     Just 2 things I ask, Just give me a little     *
 *     Credit in your code somewhere,                 *
 *     (Or just leave these comments attached)        *
 *     And Also please let me know if you are using   *
 *     and finding this code usefull in your project  *
 *     Chances are I will help you along the way.     *
 *                                                    *
 ******************************************************/

using System;
using System.IO;
using System.Reflection;

namespace TrrntzipDN
{
    class Program
    {
        public static bool NoRecursion;
        public static bool ForceReZip;
        private static bool _guiLaunch;

        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("");
                Console.WriteLine("trrntzip: missing path");
                Console.WriteLine("Usage: trrntzip [OPTIONS] [PATH/ZIP FILES]");
                return;
            }

            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                if (arg.Length < 2) continue;
                if (arg.Substring(0, 1) != "-") continue;

                switch (arg.Substring(1, 1))
                {
                    case "?":
                        Console.WriteLine("TorrentZip.Net v{0}\n", Assembly.GetExecutingAssembly().GetName().Version);
                        Console.WriteLine("Copyright (C) 2013 GordonJ");
                        Console.WriteLine("Homepage : http://www.romvault.com/trrntzip\n");
                        Console.WriteLine("Usage: trrntzip [OPTIONS] [PATH/ZIP FILE]\n");
                        Console.WriteLine("Options:\n");
                        Console.WriteLine("-? : show this help");
                        Console.WriteLine("-s : prevent sub-directory recursion");
                        Console.WriteLine("-f : force re-zip");
                        Console.WriteLine("-v : show version");
                        Console.WriteLine("-g : pause when finished");
                        return;
                    case "s":
                        NoRecursion = true;
                        break;
                    case "f":
                        ForceReZip = true;
                        break;
                    case "v":
                        Console.WriteLine("TorrentZip v{0}", Assembly.GetExecutingAssembly().GetName().Version);
                        break;
                    case "g":
                        _guiLaunch = true;
                        break;

                }
            }

            ProcessZip pz = new ProcessZip();
            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                if (arg.Length < 2) continue;
                if (arg.Substring(0, 1) == "-") continue;

                Console.WriteLine(arg);

             
                string dir = Path.GetDirectoryName(arg);
                if (String.IsNullOrEmpty(dir)) dir = Environment.CurrentDirectory;

                string filename = Path.GetFileName(arg);
                if (String.IsNullOrEmpty(filename)) filename = "*.zip";

                Console.WriteLine(dir + " | " + filename);
                DirectoryInfo dirInfo = new DirectoryInfo(dir);
                FileInfo[] fileInfo = dirInfo.GetFiles(filename);

                foreach (FileInfo file in fileInfo)
                {
                    string ext = Path.GetExtension(file.FullName);
                    if (!String.IsNullOrEmpty(ext) && ext.ToLower()==".zip")
                        pz.Process(file);
                }

                if (Directory.Exists(arg))
                {
                    pz.ProcessDir(arg);
                }
            }

            if (_guiLaunch) Console.ReadLine();
        }
    }
}
